<?php
/**
 * Created by lzl
 * Date: 2021 2021/4/7
 * Time: 16:14
 */
namespace App\Http\Controllers\Api;

use App\Forms\ExpressForms;
use App\Services\Api\CartServices;
use App\Services\Api\OrderServices;


class OrderController extends Controller
{

    /**
     * 添加购物车
     * @return mixed
     * @throws \App\Exceptions\RequestException
     */
    public function addCart()
    {
        $rules = [
            'goods_id'     => 'required|integer',
            'count'        => 'required|integer',  //商品数量
//            'sku'          => 'nullable',  //多规格情况下数组
            'attribute_id' => 'required|integer', //单规格规格id
            'type'         => 'nullable', //默认覆盖，有值则累加
        ];

        $this->validateInput($rules);

        return $this->success((new CartServices())->addToCart($this->validated));
    }


    /**
     * 删除购物车
     * @return mixed
     * @throws \App\Exceptions\RequestException
     */
    public function deleteCart()
    {
        $rules = [
            'cart_ids' => 'required',
        ];

        $this->validateInput($rules);

        return $this->success((new CartServices())->deleteCart($this->validated));
    }


    /**
     * 购物车列表
     * @return mixed
     * @throws \App\Exceptions\RequestException
     */
    public function cartList()
    {
        $rules = [
            'page'  => 'required|integer',
            'limit' => 'required|integer'
        ];

        $this->validateInput($rules);

        return $this->successOrNodata((new CartServices())->cartList($this->validated));
    }


    /**
     * 购物车数量
     * @return mixed
     */
    public function cartCount()
    {
        return $this->success((new CartServices())->cartCount());
    }


    /**
     * 购物车订单前预览
     * @return mixed
     * @throws \App\Exceptions\RequestException
     */
    public function cartPreview()
    {
        $rules = [
            'cart_ids' => 'required|array',
        ];

        $this->validateInput($rules);

        return $this->success((new OrderServices())->cartPreview($this->validated));
    }


    /**
     * 直接提交订单前预览
     * @return mixed
     * @throws \App\Exceptions\RequestException
     */
    public function submitPreview()
    {
        $rules = [
            'attribute_id' => 'required|integer',
            'count'        => 'required|integer',
        ];

        $this->validateInput($rules);

        return $this->success((new OrderServices())->submitPreview($this->validated));
    }


    /**
     * 立即支付 -》 生成订单
     * @return mixed
     * @throws \App\Exceptions\RequestException
     * @throws \Throwable
     */
    public function submit()
    {
        $rules = [
            'goods'      => 'required|array',
            'address_id' => 'required|integer',
            'pay_amount' => 'required', //用于效验实际付款金额与前台展示金额是否一致（包含快递金额），订单金额后台需另行计算
            'message'    => 'present',
            'source'     => 'present', //记录渠道
            'source_id'  => 'present', //记录插件信息
        ];

        $this->validateInput($rules);

        return $this->success((new OrderServices())->submit($this->validated));
    }


    /**
     * 商城订单异步回调
     * @throws \EasyWeChat\Kernel\Exceptions\Exception
     */
    public function notify()
    {
        return (new OrderServices())->notify();
    }


    /**
     * 确认支付
     * @return mixed
     * @throws \App\Exceptions\RequestException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function confirmPay()
    {
        $rules = [
            'id' => 'required|integer'
        ];

        $this->validateInput($rules);

        return $this->success((new OrderServices())->confirmPay($this->validated));
    }


    /**
     * 取消订单
     * @return mixed
     * @throws \App\Exceptions\RequestException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function cancel()
    {
        $rules = [
            'id' => 'required|integer'
        ];

        $this->validateInput($rules);

        return $this->success((new OrderServices())->cancel($this->validated));
    }


    /**
     * 订单列表
     * @return mixed
     * @throws \App\Exceptions\RequestException
     */
    public function orderList()
    {
        $rules = [
            'page'   => 'required|integer',
            'limit'  => 'required|integer',
            'status' => 'nullable',
        ];

        $this->validateInput($rules);


        return $this->successOrNodata((new OrderServices())->getOrderList($this->validated));
    }


    /**
     * 订单详情
     * @return mixed
     * @throws \App\Exceptions\RequestException
     */
    public function orderDetail()
    {
        $rules = [
            'id' => 'required|integer'
        ];

        $this->validateInput($rules);

        return $this->success((new OrderServices())->orderDetail($this->validated['id']));
    }


    /**
     * 物流信息
     * @return mixed
     * @throws \App\Exceptions\RequestException
     */
    public function orderExpress()
    {
        $rules = [
            'id' => 'required|integer'
        ];

        $this->validateInput($rules);

        return $this->success((new OrderServices())->orderExpress($this->validated['id']));
    }


    /**
     * 确认收货
     * @return mixed
     * @throws \App\Exceptions\RequestException
     */
    public function confirmCollect()
    {
        $rules = [
            'id' => 'required|integer'
        ];

        $this->validateInput($rules);

        return $this->success((new OrderServices())->confirmCollect($this->validated));
    }


}